<?php
/**
 * Created by PhpStorm.
 * User: DongYao
 * Date: 2017/11/21
 * Time: 上午11:31
 */

namespace XmtApp\Modules\Admin;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Route;

class AdminServiceProvider extends ServiceProvider
{

    protected $defer = false;

    /**
     * The controller namespace for the application.
     *
     * @var string|null
     */
    protected $namespace = "XmtApp\Modules\Admin\Controllers";


    /**
     * boot
     */
    public function boot()
    {
        // 设置模块路由配置
        $this->mapWebRoutes('admin');


        // 设置模块路由配置
        $this->mapApiRoutes('admin/api');

        // 设置模块的视图目录
        $this->loadViewsFrom(__DIR__.'/../resources/views', 'admin');

        // 设置模块语言包目录
        $this->loadTranslationsFrom(__DIR__.'/../resources/translations', 'admin');

        // 设置数据迁移模块
        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');

        // 设置publishes
        $this->setPublishes();

        // 合并config
        $this->mergeConfigFrom(__DIR__.'/../config/admin.php', 'admin');
    }

    /**
     * Define the "web" routes for the module.
     * @param string $prefix
     * @return void
     */
    protected function mapWebRoutes(string $prefix)
    {
        Route::prefix($prefix)->middleware('web')
            ->namespace($this->namespace)
            ->group(__DIR__.'/../routes/web.php');
    }

    /**
     * Define the "api" routes for the module.
     * @param string $prefix
     * @return void
     */
    protected function mapApiRoutes(string $prefix)
    {
        Route::prefix($prefix)->middleware('api')
            ->namespace("{$this->namespace}\Api")
            ->group(__DIR__.'/../routes/api.php');
    }


    /**
     * define the publishes for the module
     */
    protected function setPublishes()
    {
        $this->publishes([
            __DIR__.'/../config/admin.php' => config_path('admin.php')
        ], 'admin-config');
    }
}